﻿using C1.WPF;
using Leadtools;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Utility;
using ViewModel;
namespace SteelDetection
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WindowMain : Window
    {
        public WindowMain()
        {
            var licfile= System.IO.Path.Combine(Environment.CurrentDirectory, "lead.lic");
            
            RasterSupport.SetLicense(licfile, "08xfXshTu5b7bVDDLNFyk8eRAsBFLmcIj93l3qRpvDOhoNjYHmOJ1zKYpXqhCnFE");
            
            InitializeComponent();
            Loaded += (s, e) =>
            {
                Logger.Info("WindowMainLoaded");
            };
            
        }


        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed||e.OriginalSource is Canvas||Mouse.GetPosition(this).Y>65)
                return;
            this.DragMove();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn.Tag.ToString() == "running")
            {
                btn.Tag = "stop";
                ((sender as Button).Content as UIControl.Refresh).Pause();
            }
            else
            {
                btn.Tag = "running";
                ((sender as Button).Content as UIControl.Refresh).Resume();
            }

        }

        private void C1NumericBox_ValueChanged(object sender, PropertyChangedEventArgs<double> e)
        {
            (this.DataContext as VM_WindowMain).UpdateViewCommand_Executed(null);
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            (this.DataContext as VM_WindowMain).UpdateViewCommand_Executed(null);
        }


        private void TogglePanelButton_Click(object sender, RoutedEventArgs e)
        {
            // 设置 Popup 的位置为按钮的位置

            // 显示 Popup
            popup.IsOpen = !popup.IsOpen;
        }

        private void DoubleNumericBox_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            (this.DataContext as VM_WindowMain).UpdateViewCommand_Executed(null);
        }

        private void C1ComboBox_SelectedIndexChanged(object sender, PropertyChangedEventArgs<int> e)
        {
            togglePanelBtn.Focus();
        }
    }

    public class StringToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double result=0;
            double.TryParse(value.ToString(),out result);

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value.ToString();
            
        }
    }

    public class TempToColor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string colorstr = "#ff00ff88";
            if ((double)value > 60)
                colorstr = "#ffff003b";
            else if ((double)value > 50)
                colorstr = "#ffffb300";


            return new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(colorstr));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0;
        }
    }
}
